<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                >删除
                </el-button>
            </el-col>
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="warning"-->
            <!--          plain-->
            <!--          icon="el-icon-download"-->
            <!--          size="mini"-->
            <!--          @click="handleExport"-->
            <!--          v-hasPermi="['system:gateway:export']"-->
            <!--        >导出-->
            <!--        </el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="序号" align="center" type="index" :index="calculateIndex"/>
            <el-table-column label="订单编号" align="center" prop="orderNo"/>
            <el-table-column label="订单标题" align="center" prop="orderName"/>
            <el-table-column label="创建时间" align="center" prop="createTime">
                <template v-slot="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单金额" align="center" prop="orderPrice">
                <template v-slot="scope">
                    <span>{{ scope.row.orderPrice / 100 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="实际付款" align="center" prop="payPrice">
                <template v-slot="scope">
                    <span>{{ scope.row.payPrice / 100 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="付款时间" align="center" prop="payTime">
                <template v-slot="scope">
                    <span>{{ parseTime(scope.row.payTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单类型" align="center" prop="orderTypeText"/>
            <el-table-column label="订单状态" align="center" prop="orderStatus"/>
            <el-table-column label="订单有效期" align="center" prop="timeExpire">
                <template v-slot="scope">
                    <span>{{ parseTime(scope.row.timeExpire) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="用户" align="center" prop="createBy"/>
            <el-table-column label="描述" align="center" prop="attachText"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <!--          <el-button-->
                    <!--            size="mini"-->
                    <!--            type="text"-->
                    <!--            icon="el-icon-delete"-->
                    <!--            @click="handleDelete(scope.row)"-->
                    <!--            v-hasPermi="['system:gateway:remove']"-->
                    <!--          >删除</el-button>-->
                    <el-button
                        size="mini"
                        type="text"
                        @click="showPayBefore(scope.row)"
                    >充值
                    </el-button>

                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!--        <el-dialog title="充值网关"-->
        <!--                   :visible.sync="payBeforeOpen"-->
        <!--                   width="500px"-->
        <!--                   append-to-body-->
        <!--        >-->
        <!--            <PayBefore/>-->
        <!--        </el-dialog>-->

    </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { listOrder } from '@/api/order/order'

import '@/utils/directives.js'

export default {
    name: 'Gateway',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            orderList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                gatewayModel: null,
                gatewayNumber: null,
                gatewayName: null,
                gatewayLongitude: null,
                gatewayLatitude: null,
                gatewaySetupAddress: null,
                projectId: null,
                gpsLongitude: null,
                gpsLatitude: null,
                communityNumber: null,
                baseStationNumber: null,
                online: null,
                serverOnline: null,
                gatewayStatus: null,
                belongCloudControl: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            globalLoading: false,
            payBeforeOpen: false
        }
    },
    components: {},
    created() {
        this.getList()
    },
    computed: {
        ...mapGetters([
            'projectList'
        ])
    },
    methods: {
        calculateIndex(index) {
            const pageNum = this.queryParams.pageNum
            const pageSize = this.queryParams.pageSize
            return index + 1 + (pageNum - 1) * pageSize
        },
        showPayBefore(row) {
            this.payBeforeOpen = true
        },
        getList() {
            this.loading = true
            listOrder(this.queryParams).then(response => {
                this.orderList = response.rows
                this.total = response.total
                this.loading = false
            })
        },

        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                gatewayId: null,
                gatewayModel: null,
                gatewayNumber: null,
                gatewayName: null,
                gatewayLongitude: null,
                gatewayLatitude: null,
                gatewaySetupAddress: null,
                projectId: this.projectId,
                gpsLongitude: null,
                gpsLatitude: null,
                communityNumber: null,
                baseStationNumber: null,
                online: null,
                serverOnline: null,
                gatewayStatus: null,
                belongCloudControl: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.gatewayId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加网关'

        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()

            const gatewayId = row.gatewayId || this.ids[0]

            getGateway(gatewayId).then(response => {
                this.form = response.data
                this.open = true
                this.title = '修改网关'

            })
        },

        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {

                    if (this.form.gatewayId != null) {
                        updateGateway(this.form).then(response => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addGateway(this.form).then(response => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const gatewayIds = row.gatewayId || this.ids
            this.$modal.confirm('是否确认删除网关编号为"' + gatewayIds + '"的数据项？').then(function() {
                return delGateway(gatewayIds)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess('删除成功')
            }).catch(() => {
            })
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/gateway/export', {
                ...this.queryParams
            }, `gateway_${new Date().getTime()}.xlsx`)
        }

    }
}
</script>
